
import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/Gedan.dart';

class GedanStore extends Store {
  List<Gedan> _gedanList = List();
  List<AbstractSong> _currentSongs = List();

  GedanStore(){
    triggerOnAction(changeGedanList, (List<Gedan> gedans){
      _gedanList = gedans;
    });
    triggerOnAction(appendGedanList, (Gedan gedan){
      _gedanList.add(gedan);
    });
    triggerOnAction(deleteGedanSongByName, (Gedan gedan){
      List<Gedan> _gedanList = this._gedanList;
      _gedanList.removeWhere((v) => v.gdName == gedan.gdName );
    });
    triggerOnAction(subGedanCount, (int gedanId){
      int index = _gedanList.indexWhere((item) => item.id == gedanId);
      if(index == -1){
        return ;
      }
      _gedanList[index].count = _gedanList[index].count -1;
    });
    triggerOnAction(changeGedanSongs, (List<AbstractSong> songs){
      _currentSongs = songs;
    });
  }

  List<Gedan> get gedanList => _gedanList;
  List<AbstractSong> get currentSongs => _currentSongs;
}

final StoreToken gedanToken = StoreToken(GedanStore());

final Action<List<Gedan>> changeGedanList = Action();
final Action<Gedan> appendGedanList = Action();
final Action<Gedan> deleteGedanSongByName = Action();
final Action<int> subGedanCount = Action();
final Action<List<AbstractSong>> changeGedanSongs = Action();